package org.algorithm.二师兄.链表;

import org.algorithm.common.ListNode;

/**
 * @Date 2022/2/16 17:17
 */
public class 快慢指针寻找中间节点 {

    public static void main(String[] args) {
        ListNode build = ListNode.build(1, 3, 4, 7, 2, 6);
        ListNode.print(searchMiddleNode(build));
    }

    public static ListNode searchMiddleNode(ListNode listNode){
        if (listNode == null || listNode.next == null) return listNode;

        ListNode slow = listNode;
        ListNode fast = listNode;
        // 快慢指针经典做法
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }
}
